﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

//using Excel = Microsoft.Office.Interop.Excel;
//using Microsoft.Office.Core;
//using Microsoft.Office.Interop.Excel;

namespace RecipeRecommender
{
    class HelperUtil
    {
        public static Dictionary<string, int> ingredientMap = new Dictionary<string, int>();
        private const string FILE_NAME1 = "CBRData\\TempIngredientData.txt";
        private const string FILE_NAME2 = "CBRData\\TempIngredientDataCount.txt";

        public static void printAllIngredients()
        {

            if (!File.Exists(FILE_NAME1))
            {
                Console.WriteLine("{0} does not exist.", FILE_NAME1);
            }
            using (StreamReader sr = File.OpenText(FILE_NAME1))
            {
                String input;
                while ((input = sr.ReadLine()) != null)
                {
                    String[] words = input.Split(',');
                    foreach (string word in words)
                    {
                        if (ingredientMap.ContainsKey(word.Trim().ToLower()))
                        {
                            ingredientMap[word.Trim().ToLower()]++;
                        }
                        else
                        {
                            ingredientMap.Add(word.Trim().ToLower(), 1);
                        }
                    }
                }
                Console.WriteLine("The end of the stream has been reached.");

            }

            ingredientMap = (from entry in ingredientMap orderby entry.Value ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);

            /*
            List<KeyValuePair<string, int>> ingredientList = ingredientMap.ToList();

            ingredientList.Sort(
                delegate(KeyValuePair<string, int> firstPair, KeyValuePair<string, int> nextPair)
                {
                    return firstPair.Value.CompareTo(nextPair.Value);
                }
            );
            */

            StringBuilder sb = new StringBuilder();
            foreach (string key in ingredientMap.Keys)
            {
                sb.AppendLine(key + " : " + ingredientMap[key]);
            }
            System.IO.File.WriteAllText(FILE_NAME2, sb.ToString());
        }


 /*      
        public static void createRecipeFiles()
        {
            string Path = "C:\\CBRRecipeProject\\recipe.xls";
            string InstructionDirectory = "C:\\CBRRecipeProject\\RecipeRecommender\\CBRData\\Recipe\\Instructions";
            //String Path = @"../../CBRData/RecipeData.xls";
            Excel.Application app = new Application();
            Excel.Workbook workBook = app.Workbooks.Open(Path, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;


            object noIndex = 1;
            object nameIndex = 3;
            object cuisineIndex = 4;
            object mealTypeIndex = 5;
            object cookingMethodIndex = 6;
            object ingredientsIndex = 7;
            object spicyIndex = 8;
            object halalIndex = 9;
            object healthyIndex = 10;
            object vegetarianIndex = 11;
            object cookingTimeIndex = 12;
            object servingPortionIndex = 13;

            object rowIndex;
            for (int i = 1; i <= 120; i++)
            {
                rowIndex = i;
                try
                {
                    string no = ((Excel.Range)workSheet.Cells[rowIndex, noIndex]).Value2.ToString();
                    string name = ((Excel.Range)workSheet.Cells[rowIndex, nameIndex]).Value2.ToString();
                    string cuisine = ((Excel.Range)workSheet.Cells[rowIndex, cuisineIndex]).Value2.ToString();
                    string mealType = ((Excel.Range)workSheet.Cells[rowIndex, mealTypeIndex]).Value2.ToString();
                    string cookingMethod = ((Excel.Range)workSheet.Cells[rowIndex, cookingMethodIndex]).Value2.ToString();
                    string ingredients = ((Excel.Range)workSheet.Cells[rowIndex, ingredientsIndex]).Value2.ToString();
                    string spicy = ((Excel.Range)workSheet.Cells[rowIndex, spicyIndex]).Value2.ToString();
                    string halal = ((Excel.Range)workSheet.Cells[rowIndex, halalIndex]).Value2.ToString();
                    string healthy = ((Excel.Range)workSheet.Cells[rowIndex, healthyIndex]).Value2.ToString();
                    string vegetarian = ((Excel.Range)workSheet.Cells[rowIndex, vegetarianIndex]).Value2.ToString();
                    string cookingTime = ((Excel.Range)workSheet.Cells[rowIndex, cookingTimeIndex]).Value2.ToString();
                    string servingPortion = ((Excel.Range)workSheet.Cells[rowIndex, servingPortionIndex]).Value2.ToString();

                    string[] ingredientArr = ingredients.Split(',');
                    StringBuilder cookingInstructions = new StringBuilder();
                    
                    using (StreamReader sr = File.OpenText(InstructionDirectory+"\\"+i+".txt"))
                    {
                        String input;
                        while ((input = sr.ReadLine()) != null)
                        {
                            cookingInstructions.AppendLine(input);
                        }
                        Console.WriteLine("The end of the stream has been reached.");

                    }
                    

                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine(name);
                    sb.AppendLine(cuisine);
                    sb.AppendLine(mealType);
                    sb.AppendLine(cookingMethod);
                    sb.AppendLine(spicy);
                    sb.AppendLine(halal);
                    sb.AppendLine(healthy);
                    sb.AppendLine(vegetarian);
                    sb.AppendLine(cookingTime);
                    sb.AppendLine(servingPortion);
                    sb.AppendLine();
                    sb.AppendLine("Ingredients:");
                    foreach (string ingredient in ingredientArr)
                        sb.AppendLine(ingredient.Trim());
                    sb.AppendLine();
                    sb.AppendLine("Cooking Instructions:");
                    sb.AppendLine(cookingInstructions.ToString());

                    String filename = @"../../CBRData/Recipe_/" + no + ". " + name + ".txt";
                    System.IO.File.WriteAllText(filename, sb.ToString());
                   
                }
                catch (Exception ex)
                {
                     //Log the exception and quit...
                    app.Quit();
                    Console.WriteLine(ex.Message);
                }
            }
        }
*/
    }
}

